
#coding:utf8

__author__='zhaoxp'

import socket
import platform
import datetime

import psutil


class HostStats(object):
    
    def __init__(self,level='basic'):
        '''
        level: basic(default),advanced
        '''
        self.ostype=platform.system() # Linux or Windows
        self.level=level
        print 'level =',self.level
    
    def get_host_stats(self):
        data={}
        data['hostname']=socket.gethostname()
        data['ip']=socket.gethostbyname(socket.gethostname())
        data['datetime']=datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S %f')
        data['boottime']=datetime.datetime.fromtimestamp(psutil.boot_time()).strftime('%Y-%m-%d %H:%M:%S %f')
        data['ostype']=self.ostype
        data['cpu']=self._get_cpu_info()
        data['memory']=self._get_mem_info()
        data['disk']=self._get_disk_info()
        data['io']=self._get_io_info()
        return data

    def _get_cpu_info(self):
        '''
        collect CPU stats
        psutil : cpu_count cpu_percent cpu_stats cpu_times cpu_times_percent
        '''
        cdata={}
        cdata['lcount']=psutil.cpu_count()
        cdata['pcount']=psutil.cpu_count(logical=False)
        cdata['percent']=psutil.cpu_percent()
        cp=psutil.cpu_times_percent(interval=5)
        cdata['interval']=5
        cdata['user']=cp.user
        cdata['system']=cp.system
        cdata['idle']=cp.idle
        if self.ostype=='Windows':
            cdata['interrupt']=cp.interrupt
            cdata['dpc']=cp.dpc
        elif self.ostype=='Linux':
            cdata['iowait']=cp.iowait
            cdata['nice']=cp.nice
            cdata['irq']=cp.irq
            cdata['softirq']=cp.softirq
            cdata['steal']=cp.steal
            cdata['guest']=cp.guest
        if self.level=='advanced':
            stat=psutil.cpu_stats()
            cdata['ctx_switches']=stat.ctx_switches
            cdata['interrupts']=stat.interrupts
            cdata['soft_interrupts']=stat.soft_interrupts
            cdata['syscalls']=stat.syscalls
            '''
            ctms=psutil.cpu_times()
            cdata['user']=ctms.user
            cdata['system']=ctms.system
            cdata['idle']=ctms.idle
            if self.ostype=='Windows':
                cdata['interrupt']=ctms.interrupt
                cdata['dpc']=ctms.dpc
            elif self.ostype=='Linux':
                cdata['iowait']=ctms.iowait
                cdata['nice']=ctms.nice
                cdata['irq']=ctms.irq
                cdata['softirq']=ctms.softirq
                cdata['steal']=ctms.steal
                cdata['guest']=ctms.guest
            '''
        return cdata

    def _get_mem_info(self):
        mdata={}
        mem=psutil.virtual_memory()
        mdata['total']=mem.total #/1024/1024/1024
        mdata['used']=mem.used #/1024/1024/1024
        #mdata['available']=mem.available #/1024/1024/1024
        mdata['percent']=mem.percent
        if self.ostype=='Linux' and self.level=='advanced':
            mdata['active']=mem.active
            mdata['inactive']=mem.inactive
            mdata['buffers']=mem.buffers
            mdata['cached']=mem.cached
            mdata['shared']=mem.shared
        sm=psutil.swap_memory()
        mdata['swap_total']=sm.total
        mdata['swap_used']=sm.used
        mdata['swap_free']=sm.free
        mdata['swap_percent']=sm.percent
        if self.level=='advanced':
            mdata['swap_sin']=sm.sin
            mdata['swap_sout']=sm.sout
        return mdata

    def _get_disk_info(self):
        ddata={}
        usage={}
        for pt in psutil.disk_partitions():
            try:
                ug=psutil.disk_usage(pt.mountpoint)
                usage[pt.mountpoint]=ug.percent
            except:
                #print 'disk_usage exception:',pt
                pass
        ddata['usage']=usage
        if self.level=='advanced':
            dic=psutil.disk_io_counters()
            ddata['read_count']=dic.read_count
            ddata['write_count']=dic.write_count
            ddata['read_bytes']=dic.read_bytes
            ddata['write_bytes']=dic.write_bytes
            ddata['read_time']=dic.read_time
            ddata['write_time']=dic.write_time
            if self.ostype=='Linux':
                ddata['read_merged_count']=dic.read_merged_count
                ddata['write_merged_count']=dic.write_merged_count
                ddata['busy_time']=dic.busy_time
        return ddata

    def _get_io_info(self):
        idata={}
        io=psutil.net_io_counters()
        idata['bytes_recv']=io.bytes_recv
        idata['bytes_sent']=io.bytes_sent
        idata['packets_sent']=io.packets_sent
        idata['packets_recv']=io.packets_recv
        idata['errin']=io.errin
        idata['errout']=io.errout
        idata['dropin']=io.dropin
        idata['dropout']=io.dropout
        return idata

if __name__=='__main__':
    print(HostStats(level='advanced').get_host_stats())
